package com.eight.travel.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Q小旭
 * @version 1.0
 */
public class JWTUtils {

    public static String createJWT(Map map){
        //声明头部
        Map map1 = new HashMap<>();

        map1.put("typ","JWT");
        map1.put("alg","HS256");
        //声明算法
        Algorithm algorithm = Algorithm.HMAC256("2209");
        //生成token
        String sign = JWT.create().withHeader(map)
                //签发人
                .withIssuer("qf")
                .withAudience("users")
                .withSubject("auth")
                .withClaim("users", map)
                .sign(algorithm);
        return sign;
    }

    public static Map verfiyJWT(String token){
        Algorithm algorithm = Algorithm.HMAC256("2209");
        JWTVerifier build = JWT.require(algorithm).build();

        try {
            DecodedJWT verify = build.verify(token);
            Map<String, Object> users = verify.getClaim("users").asMap();
            return users;
        }catch (Exception e){
            return null;
        }

    }
}
